package com.tedu.openmedical.caseInfo.service.imp;

import com.tedu.openmedical.caseInfo.mapper.CaseMapper;
import com.tedu.openmedical.caseInfo.pojo.dto.CaseQuery;
import com.tedu.openmedical.caseInfo.pojo.vo.CaseVo;
import com.tedu.openmedical.caseInfo.service.CaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.List;
@Service
@Slf4j
public class CaseServiceImpl implements CaseService {
    @Autowired
    private CaseMapper caseMapper;
    @Override
    public List<CaseVo> select(CaseQuery caseQuery) {
        log.debug("查询病案查询 caseQuery={}",caseQuery);
        List<CaseVo> CaseVOList=caseMapper.selectById(caseQuery);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate currentDate = LocalDate.now();
        for (CaseVo caseVo:CaseVOList){
            LocalDate birthDate = LocalDate.parse(caseVo.getBirthday(), formatter);
            Period period = Period.between(birthDate, currentDate);
            if (period.getYears()==0){
                caseVo.setAge(period.getMonths()+"月");
            }else {
                caseVo.setAge(period.getYears()+"岁");
            }
        }
        return CaseVOList;
    }
}
